package study.java.jdk.jdk8._1_stream.collectors.grouping_by;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import study.java.jdk.jdk8.DataUtils;
import study.java.jdk.jdk8.User;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class _1_groupingBy_Test {

    private List<User> userLt = DataUtils.buildUserLt();

    /**
     * ==============================
     * （1）分组
     * ==============================
     *
     * @param classifier Function（分类器）
     */
    @Test
    public void test() {
        Map<String, List<User>> dest = userLt.stream()
                .collect(Collectors.groupingBy(User::getCode));
        log.info("{}", dest);
    }

    /**
     * 属性组合分组
     */
    @Test
    public void test1() {
        Map<String, List<User>> dest = userLt.stream()
                .collect(Collectors.groupingBy(u -> u.getCode() + "_" + u.getName()));
        log.info("{}", dest);
    }

    /**
     * 根据条件自定义分组
     */
    @Test
    public void test2() {
        Map<String, List<User>> dest = userLt.stream()
                .collect(Collectors.groupingBy(u -> {
                    if (u.getAge() <= 12) {
                        return "儿童";
                    } else {
                        return "成年";
                    }
                }));
        log.info("{}", dest);
    }
}
